<?php

require_once( 'flexical/model/format.php' );

require_once( 'flexical/admin/view.php' );
require_once( 'flexical/admin/format-response.php' );

abstract class Flexical_Admin_Basic_Format_View extends Flexical_Admin_View
{
	public function render_format_form( $format, $request_type, $submit_button_text, $cancel_enabled, $mode )
	{
		$info = new Flexical_Admin_Form_Table_Info();

		if ( $format == null )
		{
			$format = new Flexical_Model_Format();
		}

		else
		{
			$info->rows[ 'ID' ] = $format->id;
			$info->hiddens[ 'format_id' ] = $format->id;
		}

		switch ( $mode )
		{
			case 'readonly':
				$info->rows[ 'Title' ] = esc_html( $format->title );
				$info->rows[ 'Start of listing code' ] = esc_html( $format->start_listing_code );
				$info->rows[ 'Event code' ] = esc_html( $format->event_code );
				$info->rows[ 'End of listing code' ] = esc_html( $format->end_listing_code );
				$info->rows[ 'Date format' ] = esc_html( $format->date_format );
				$info->rows[ 'Time format' ] = esc_html( $format->time_format );
				break;

			case 'readwrite':
				$info->rows[ 'Title' ] = "<input type=\"text\" name=\"format_title\" size=\"50\" value=\"" . esc_html( $format->title ) . "\" />\n";
				$info->rows[ '' ] = '<p>Variables that can be used in any of the following fields:</p>' . $this->get_format_variables();
				$info->rows[ 'Start of listing code' ] = "<textarea name=\"format_start_listing_code\" rows=\"5\" cols=\"80\">" .
					esc_html( $format->start_listing_code ) . "</textarea>\n";
				$info->rows[ 'Event code' ] = "<textarea name=\"format_event_code\" rows=\"5\" cols=\"80\">" .
					esc_html( $format->event_code ) . "</textarea>\n";
				$info->rows[ 'End of listing code' ] = "<textarea name=\"format_end_listing_code\" rows=\"5\" cols=\"80\">" .
					esc_html( $format->end_listing_code ) . "</textarea>\n";
				$info->rows[ 'Date format' ] = "<input type=\"text\" name=\"format_date_format\" size=\"50\" value=\"" .
					esc_html( $format->date_format ) . "\" />\n";
				$info->rows[ 'Time format' ] = "<input type=\"text\" name=\"format_time_format\" size=\"50\" value=\"" .
					esc_html( $format->time_format ) . "\" />\n";
				break;

			default:
				throw new Exception( "Unsupported mode: " . $mode );
		}

		$info->request_type = $request_type;
		$info->submit_button_text = $submit_button_text;
		$info->cancel_enabled = $cancel_enabled;

		$this->render_form_table( $info );
	}

	private function get_format_variables()
	{
		return '${title}, ${description}, ${location}, ${map_url}, ${start_time}, ${end_time}, ${start_date}, ${end_date}, ${feed_title}, ${query_title}, ${listing_title}';
	}
}

class Flexical_Admin_Format_View extends Flexical_Admin_Basic_Format_View
{
	public function get_selected_tab()
	{
		return $this->FORMATS_TAB;
	}

	public function get_messages( $response )
	{
		$messages = array();

		switch ( get_class( $response ) )
		{
			case 'Flexical_Admin_Show_Formats_Response':

				if ( $response->canceled )
				{
					$messages[] = new Flexical_Admin_Info_Message( "Action canceled." );
				}

				break;

			case 'Flexical_Admin_Delete_All_Formats_Response':

				if ( $response->confirmed )
				{
					$messages[] = new Flexical_Admin_Info_Message( "All formats were deleted." );
				}

				else
				{
					$messages[] = new Flexical_Admin_Error_Message( "The confirmation box for that action was not checked." );
				}

				break;

			case 'Flexical_Admin_Create_Format_Response':
				$format = $response->format;
				$messages[] = new Flexical_Admin_Info_Message( "Created format " . $format->id . ': ' . $format->title );
				break;

			case 'Flexical_Admin_Update_Format_Response':
				$format = $response->format;
				$messages[] = new Flexical_Admin_Info_Message( "Updated format " . $format->id . ': ' . $format->title );
				break;

			case 'Flexical_Admin_Delete_Format_Response':
				$format = $response->format;
				$messages[] = new Flexical_Admin_Info_Message( "Deleted format " . $format->id . ': ' . $format->title );
				break;
			
			case 'Flexical_Admin_View_Format_Response':
				$format = $response->format;
				$messages[] = new Flexical_Admin_Info_Message( $this->build_view_format( $format ) );
				break;

			case 'Flexical_Admin_Cant_Delete_Format_In_Use_Response':
				$text = '<p>Format ' . $response->format->id . ' "' . $response->format->title . '" is used in one or more listings. ';
				$text .= "You must remove this format from the listings or remove the listings before the format can be removed.</p>\n";
				$text .= "<p>Listings that use this format:</p>\n";
				$text .= $this->build_listings_in_use_table( $response->listings_using_format );
				$messages[] = new Flexical_Admin_Error_Message( $text );
				break;

			case 'Flexical_Admin_Cant_Delete_Formats_In_Use_Response':
				$messages[] = new Flexical_Admin_Error_Message( "You must remove all of the listings before you can remove all of the formats." );
				break;

			default:
				break;
		}

		return $messages;
	}

	public function render_details( $response )
	{
		$this->render_format_table( $response->formats );
		echo( "<p>&nbsp;</p>" );
		$this->render_create_format_form();
		echo( "<p>&nbsp;</p>" );

		if ( sizeof( $response->formats ) > 0 )
		{
			$this->render_delete_all_formats_form();
			echo( "<p>&nbsp;</p>" );
		}
	}

	private function render_format_table( $formats )
	{
		echo( "<h3>Formats</h3>\n" );

		$headers = array( 'ID', 'Title', 'Date Format', 'Time Format', 'Actions' );

		echo( "<table class=\"widefat\">\n" );
		echo( "<thead><tr><th>" . implode( "</th><th>", $headers ) . "</th></tr></thead>\n" );

		foreach ( $formats as $id => $format )
		{
			$data = array( $format->id, $format->title, $format->date_format, $format->time_format, $this->get_actions( $format ) );

			echo( "<tr scope=\"row\"><td align=\"left\" valign=\"top\">" . implode( "</td><td align=\"left\" valign=\"top\">", $data ) . "</td></tr>\n" );
		}

		echo( "<tfoot><tr><th>" . implode( "</th><th>", $headers ) . "</th></tr></tfoot>\n" );
		echo( "</table>\n" );
	}

	private function get_actions( $format )
	{
		return
			"<a href=\"" . $this->get_request_url( 'view_format', array( 'format_id' => $format->id ) ) . "\">View</a>" . " | " .
			"<a href=\"" . $this->get_request_url( 'edit_format', array( 'format_id' => $format->id ) ) . "\">Edit</a>" . " | " .
			"<a href=\"" . $this->get_request_url( 'confirm_delete_format', array( 'format_id' => $format->id ) ) . "\">Delete</a>";
	}

	private function render_create_format_form()
	{
		echo( "<h3>Create a New Format</h3>\n" );

		$this->render_format_form( null, 'create_format', 'Create Format', false, 'readwrite' );
	}

	private function render_delete_all_formats_form()
	{
		echo( "<h3>Delete All Formats</h3>\n" );
		echo( "<p>This action will delete all of the formats. <b>WARNING: This action cannot be undone.</b></p>" );

		$info = new Flexical_Admin_Form_Table_Info();
		$info->request_type = 'delete_all_formats';
		$info->submit_button_text = 'Delete All Formats';
		$info->cancel_enabled = false;
		$info->rows[ 'Confirm' ] = $this->get_confirmation_checkbox();

		$this->render_form_table( $info );
	}

	private function build_view_format( $format )
	{
		$data = array(
			'ID' => 'id',
			'Title' => 'title',
			'Start listing code' => 'start_listing_code',
			'Event code' => 'event_code',
			'End listing code' => 'end_listing_code',
			'Date format' => 'date_format',
			'Time format' => 'time_format'
		);

		$content = '';
		$content .= "<h3>View Format</h3>\n";
		$content .= "<table class=\"form-table\">\n";

		foreach ( $data as $label => $property )
		{
			eval( "\$value = \$format->$property;" );

			$content .= "<tr scope=\"row\"><th>" . $label . "</th><td>" . esc_html( $value ) . "</td></tr>\n";
		}

		$content .= "</table><p>&nbsp;</p>\n";

		return $content;
	}
}

class Flexical_Admin_Confirm_Delete_Format_View extends Flexical_Admin_Basic_Format_View
{
	public function get_selected_tab()
	{
		return $this->FORMATS_TAB;
	}

	public function render_details( $response )
	{
		echo( "<h3>Are you sure you want to delete this format?</h3>\n" );
		$this->render_format_form( $response->format, 'delete_format', 'Delete Format', true, 'readonly' );
	}
}

class Flexical_Admin_Edit_Format_View extends Flexical_Admin_Basic_Format_View
{
	public function get_selected_tab()
	{
		return $this->FORMATS_TAB;
	}

	public function render_details( $response )
	{
		echo( "<h3>Edit Format</h3>\n" );
		$this->render_format_form( $response->format, 'update_format', 'Update Format', true, 'readwrite' );
	}
}

?>
